package com.luhuiguo.cms.server.handler;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.server.util.SecurityUtils;
import com.luhuiguo.cms.shared.model.UserModel;
import com.luhuiguo.cms.shared.rpc.GetCurrentUser;
import com.luhuiguo.cms.shared.rpc.GetCurrentUserResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class GetCurrentUserHandler implements ActionHandler<GetCurrentUser, GetCurrentUserResult> {


    @Override
    @PreAuthorize("isAuthenticated()")
    public GetCurrentUserResult execute(GetCurrentUser action, ExecutionContext context) throws ActionException {

        UserModel model = MapperUtils.map(SecurityUtils.getCurrentUser(), UserModel.class);

        return new GetCurrentUserResult(model);
    }

    @Override
    public Class<GetCurrentUser> getActionType() {
        return GetCurrentUser.class;
    }

    @Override
    public void rollback(GetCurrentUser action, GetCurrentUserResult result, ExecutionContext context)
            throws ActionException {

    }

}
